import {useUsersStore} from '@/store/usersStroe.ts'
import {onBeforeMount} from "vue";
import {ElMessageBox, ElNotification} from "element-plus";
import {deleteUser, deleteUsers} from "@/api/user";


export default function () {
    const usersStore = useUsersStore();
    const createUser = () => {
        console.log(usersStore.createUserDrawer);
        usersStore.createUserDrawer = true;
    }
    const selectRowID = ref<number[]>([])
    const handleBatchDelete = () => {
        if (selectRowID.value.length == 0) {
            ElNotification({
                message: '请选择要删除的数据',
                type: 'warning'
            })
            return false
        }
        ElMessageBox.confirm("确定永久删除这" + selectRowID.value.length + "条用户数据吗？", {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
        }).then(async () => {
            const res = await deleteUsers(selectRowID.value)
            if (res.code == 200) {
                ElNotification({
                    message: '批量删除成功',
                    type: 'success'
                })
                await usersStore.getUserListFun()
            } else {
                ElNotification({
                    message: '批量删除失败',
                    type: 'error'
                })
            }
        }).catch(() => {
        })
    }
    const search = async () => {
        try {
            await usersStore.getUserListFun();
        } catch (e: any) {
            ElNotification({
                message: e,
                type: 'error',
            })
        }

    }
    const formatCATTime = (row: any) => {
        return row.CreatedAt.substring(0, 19).replace('T', ' ');  // 格式化时间
    }
    const formatUATTime = (row: any) => {
        return row.UpdatedAt.substring(0, 19).replace('T', ' ');
    }
    const formatLoginTime = (row: any) => {
        if (row.loginTime === null) {
            return '';
        }
        return row.loginTime.substring(0, 19).replace('T', ' ');
    }
    const selectedUser = (row: any) => {
        let rowId: number[] = []
        row.forEach((item: any) => {
            rowId.push(item.ID)
        })
        selectRowID.value = rowId
    }
    const selectedallUser = (row: any) => {
        let rowId: number[] = []
        row.forEach((item: any) => {
            rowId.push(item.ID)
        })
        selectRowID.value = rowId
    }
    onBeforeMount(async () => {
        try {
            await usersStore.getUserListFun();
            usersStore.userList.forEach(row => {
                usersStore.formatAvatar(row)
            })
        } catch (e: any) {
            ElNotification({
                message: e,
                type: 'error',
            })
        }
    })
    const edit = (row: any) => {
        usersStore.editUserDrawer = true;
        usersStore.editUserData.ID = row.ID;
        usersStore.editUserData.userName = row.user_name;
        usersStore.editUserData.loginName = row.login_name;
        usersStore.editUserData.age = row.age;
        usersStore.editUserData.phone = row.phone;
        usersStore.editUserData.email = row.email;
        usersStore.editUserData.sex = row.sex;
        usersStore.editUserData.status = row.status;
        usersStore.editUserData.password = row.password;
        usersStore.editUserData.roles = row.roles.map((item: any) => {
            return item.ID
        })
    }
    const del = (row: any) => {
        ElMessageBox.confirm(
            '确定永久删除用户' + row.user_name + '？',
            {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            }
        ).then(() => {
            deleteUser(row.ID).then(async (res) => {
                if (res.code == 200) {
                    ElNotification({
                        message: res.msg,
                        type: 'success',
                    })

                    await usersStore.getUserListFun();
                } else {
                    ElNotification({
                        message: res.msg,
                        type: 'error',
                    })
                }
            }).catch((err) => {
                ElNotification({
                    message: err.response.data.msg,
                    type: 'error',
                })
            })

        }).catch(() => {

        })
    }
    const handleCurrentChange = async (page: number) => {
        usersStore.filteredData.page = page;
        try {
            await usersStore.getUserListFun();
            usersStore.userList.forEach(row => {
                usersStore.formatAvatar(row)
            })
        } catch (e: any) {
            ElNotification({
                message: e,
                type: 'error',
            })
        }

    }
    const handleSizeChange = async (size: number) => {
        usersStore.filteredData.pageSize = size;
        try {
            await usersStore.getUserListFun();
            usersStore.userList.forEach(row => {
                usersStore.formatAvatar(row)
            })
        } catch (e: any) {
            ElNotification({
                message: e,
                type: 'error',
            })
        }
    }
    return {
        usersStore,
        createUser,
        handleBatchDelete,
        search,
        formatCATTime,
        formatUATTime,
        formatLoginTime,
        selectedUser,
        selectedallUser,
        edit,
        del,
        handleCurrentChange,
        handleSizeChange,
    }
}